package com.up.upzl.biz.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.up.upzl.base.service.impl.BaseServiceImpl;
import com.up.upzl.biz.domain.SysApp;
import com.up.upzl.biz.domain.SysRole;
import com.up.upzl.biz.repository.SysAppRepository;
import com.up.upzl.biz.repository.SysRoleRepository;
import com.up.upzl.biz.service.SysRoleAppService;

@Service
public class SysRoleAppServiceImpl extends BaseServiceImpl<SysRole, Long> implements SysRoleAppService {

	@Autowired
	private SysRoleRepository sysRoleRepository;

	@Autowired
	private SysAppRepository sysAppRepository;

	@Override
	public List<SysApp> findRoleApps(Long roleID) {
		SysRole role = sysRoleRepository.findOne(roleID);
		return role.getSysApps();
	}

	@Override
	public boolean saveRoleApp(Long roleID, Long[] appIDs) {
		List<SysApp> appList = sysAppRepository.findAll(Arrays.asList(appIDs));
		SysRole sysRole = sysRoleRepository.findOne(roleID);
		sysRole.setSysApps(appList);
		sysRoleRepository.save(sysRole);
		return true;
	}

	@Override
	public List<SysRole> findAllRoleList() {
		return sysRoleRepository.findAll();
	}

	@Override
	public List<SysApp> findAllApps() {
		return sysAppRepository.findAll();
	}

}
